<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="设备型号" prop="model">
        <el-input v-model="queryParams.model" placeholder="请输入" clearable style="width: 240px"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <page-table :url="url.list" ref="table" :tableForm="queryParams" :columns="columns">
      <template #action="scope">
        <el-tooltip content="修改" placement="top">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
        </el-tooltip>
      </template>
      <template #status="scope">
        <el-switch v-model="scope.row.status" v-if="scope.row.status != undefined" :active-value="1" :inactive-value="0"
          @change="handleDelete(scope.row, scope.row.status)"></el-switch>
      </template>
    </page-table>

    <!-- 添加或修改设备配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="供应商电话" prop="supplierTel">
          <el-input v-model="form.supplierTel" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="通信协议" prop="communicationProtocol">
          <el-input v-model="form.communicationProtocol" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Role">
import request from '@/utils/request'
const router = useRouter();
const { proxy } = getCurrentInstance();

const url = ref({
  list: 'devicesType/getPage/forManager',
  fine: 'devicesType/getOne/forManager',
  delete: 'devicesType/disableOrUse/forManager',
  add: 'devicesType/save/forManager',
  edit: 'devicesType/save/forManager'
});

const open = ref(false);
const title = ref("");
const table = ref(null);

const columns = ref([{
  key: 'name',
  title: '设备名称',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'model',
  title: '设备型号',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'devicesCount',
  title: '已对接设备数量',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'supplierName',
  title: '供应商名称',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'supplierTel',
  title: '供应商电话',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'communicationProtocol',
  title: '通信协议',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'status',
  title: '状态',
  align: 'center',
  width: 150,
  slots: {
    customKey: 'status',
  },
},
{
  title: '操作',
  key: 'action',
  slots: {
    customKey: 'action',
  },
  align: 'center',
  fixed: 'right',
  width: 150
}]);

const handleDelete = (row, status) => {
  const id = row.id
  let text = row.status == "1" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.name + '"吗?').then(function () {
    return request.get(url.value.delete, { params: { id } });
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "0" ? "1" : "0";
  });
}

const data = reactive({
  form: {},
  queryParams: {},
  rules: {
    name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
    model: [{ required: true, message: "设备型号不能为空", trigger: "blur" }],
    supplierName: [{ required: true, message: "供应商名称不能为空", trigger: "blur" }],
    supplierTel: [{ required: true, message: "供应商电话不能为空", trigger: "blur" }],
    communicationProtocol: [{ required: true, message: "通信协议不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  table.value.onload();
}
/** 搜索按钮操作 */
function handleQuery() {
  table.value.init();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {};
  proxy.resetForm("roleRef");
}
/** 添加设备 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备";
}
/** 修改设备 */
function handleUpdate(row) {
  reset();
  const id = row.id
  request.get(url.value.fine, { params: { id } }).then(response => {
    form.value = response.result;
    open.value = true;
    title.value = "修改设备";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        request.post(url.value.edit, form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        request.post(url.value.add, form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


</script>
